package com.example.cmtwo.service;

import com.example.cmtwo.bean.Commodity;
import com.example.cmtwo.bean.Userinformation;
import com.example.cmtwo.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;


@Service
public class GetCommodityservice {
    @Autowired
    private CommodityMapper commodityMapper;

    public List<Commodity> getCommodityByClass(String Many, String Class)
    {
        List<Commodity> com = new LinkedList<Commodity>();
        try {
            if (Many.equals("more"))
            {
                if (Class.equals("xx"))
                {
                    com  = commodityMapper.getMoreComByxx();    //调用新鲜
                }
                else
                {
                    com  = commodityMapper.getMoreComByClass(Class);  //调用mapper层查询数据库
                }
            }

            if (Many.equals("less"))
            {
                if (Class.equals("xx"))
                {
                    com  = commodityMapper.getComByxx();    //调用新鲜
                }
                else
                {
                    com  = commodityMapper.getComByClass(Class);  //调用mapper层查询数据库
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return com;
        }
        return com;
    }

    public Commodity getCommodityById(Integer id)
    {
        Commodity com = new Commodity();
        try {
            com  = commodityMapper.getComById(id);    //调用新鲜
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return com;
        }
        return com;
    }

    //***********By GeJian*************
    //通过商家编号提取商品信息
    public List<Commodity> getCommodityByUId(Integer id)
    {
        List<Commodity> com = new LinkedList<Commodity>();
        try {
            com  = commodityMapper.getComByUId(id);    //调用新鲜
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return com;
        }
        return com;
    }

    public Userinformation getUserinformationById(Integer id)
    {
        Userinformation com = new Userinformation();
        try {
            com  = commodityMapper.getUserinfById(id);    //利用商品的id查出卖家信息
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return com;
        }
        return com;
    }

    //***********By GeJian*************
    //修改商品信息
    public boolean updateCommoditys(Integer Cid, String Cname, Float Price, Float Originalprice, String Intro , String Classification, String Image)
    {
        try {
            commodityMapper.updateComMes(Cid, Cname, Price, Originalprice, Intro , Classification, Image);
        }catch (Exception e)
        {
            System.out.println(e);
            return false;
        }

        return true;
    }

    //***********By GeJian*************
    //删除商品
    public boolean DeleteCom(Integer Cid)
    {
        try {
            commodityMapper.deleteComMes(Cid);
        }catch (Exception e)
        {
            System.out.println(e);
            return false;
        }

        return true;
    }
}
